<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="A${entity}">


    <!-- 通用查询映射结果 -->
    <resultMap id="${entity}Result" type="${entity}">
        <#list table.fields as field>
            <#if field.keyFlag><#--生成主键排在第一位-->
                <id column="${field.name}" property="${field.propertyName}"/>
            </#if>
        </#list>

        <#list table.fields as field>
            <#if !field.keyFlag><#--生成普通字段 -->
                <result column="${field.name}" property="${field.propertyName}"/>
            </#if>
        </#list>

        <#list table.commonFields as field><#--生成公共字段 -->
            <result column="${field.name}" property="${field.propertyName}"/>
        </#list>

    </resultMap>


    <!-- 通用查询结果列 -->
    <sql id="${entity}JoinMap">
        <#list table.fields as field>
            ,${'$'}{prefix}.${field.name} as ${'$'}{prefix}_${field.name}
        </#list>
        <#list table.commonFields as field><#--生成公共字段 -->
            ,${'$'}{prefix}.${field.name} as ${'$'}{prefix}_${field.name}
        </#list>
    </sql>


    <insert id="insert${entity}" parameterType="${entity}" useGeneratedKeys="true" keyProperty="id">

        INSERT INTO `${table.name}` (
        <trim suffixOverrides=",">
            <#list table.fields as field>
                <if test="${field.propertyName}!=null">`${field.name}`,</if>
            </#list>
            <#list table.commonFields as field><#--生成公共字段 -->
                <if test="${field.propertyName}!=null">`${field.name}`,</if>
            </#list>
        </trim>
        )
        VALUE(
        <trim suffixOverrides=",">
            <#list table.fields as field>
                <if test="${field.propertyName}!=null"> ${'#'}{${field.propertyName}},</if>
            </#list>
            <#list table.commonFields as field><#--生成公共字段 -->
                <if test="${field.propertyName}!=null"> ${'#'}{${field.propertyName}},</if>
            </#list>
        </trim>
        )

    </insert>


    <insert id="insert${entity}List" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO `${table.name}` (
        <trim suffixOverrides=",">
            <#list table.fields as field>
                <if test="${field.propertyName}!=null">`${field.name}`,</if>
            </#list>
            <#list table.commonFields as field><#--生成公共字段 -->
                <if test="${field.propertyName}!=null">`${field.name}`,</if>
            </#list>
        </trim>
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                <#list table.fields as field>
                    <if test="item.${field.propertyName}!=null"> ${'#'}{item.${field.propertyName}},</if>
                </#list>
                <#list table.commonFields as field><#--生成公共字段 -->
                    <if test="item.${field.propertyName}!=null"> ${'#'}{item.${field.propertyName}},</if>
                </#list>
            </trim>
            )
        </foreach>
    </insert>

    <update id="update${entity}" parameterType="${entity}">
        update `${table.name}`
        SET
        <trim suffixOverrides=",">
            <#list table.fields as field>
                <if test="${field.propertyName}!=null">`${field.name}`= ${'#'}{${field.propertyName}},</if>
            </#list>
            <#list table.commonFields as field><#--生成公共字段 -->
                <if test="${field.propertyName}!=null">`${field.name}`= ${'#'}{${field.propertyName}},</if>
            </#list>
        </trim>
        WHERE
        id=${'#'}{id}
    </update>

    <update id="del${entity}" parameterType="${entity}">
        update `${table.name}`
        SET
        deleted = 0
        WHERE
        id=${'#'}{id}
    </update>

</mapper>
